#!/usr/bin/env python
import os, sys, glob
import unittest
import doctest
from os.path import dirname, basename, splitext, join, expanduser

TESTDIR = dirname(os.path.abspath(__file__))
MAINDIR = dirname(TESTDIR)

# Modify the paths to fit your system
if sys.platform=='win32':
    GOOGLE_PATH = "C:/Program Files/Google/google_appengine"
else:
    GOOGLE_PATH = expanduser("~/google_appengine")

EXTRA_PATHS = [
    TESTDIR,
    MAINDIR,
    GOOGLE_PATH,
    os.path.join(GOOGLE_PATH, 'lib', 'django'),
    os.path.join(GOOGLE_PATH, 'lib', 'webob'),
    os.path.join(GOOGLE_PATH, 'lib', 'yaml', 'lib'),
]
for directory in EXTRA_PATHS:
    if not directory in sys.path:
        sys.path.insert(0, directory)

APP_ID = u'test_app'
AUTH_DOMAIN = 'gmail.com'
LOGGED_IN_USER = 'test@example.com'  # set to '' for no logged in user 

def setUp(self):
   return

def initGlobs():
    """Create global variables for use in tests."""
    import os, sys
    return dict(os=os, sys=sys)

def getModules():
    """return all modules in the parent directory"""

    modules = [ splitext(basename(name))[0] for name in glob.glob(join(MAINDIR, '*.py'))]
    for mod in modules:
        if not mod.startswith('_'):
            yield __import__(mod)

def getDocTests():
    """Find doctest files in current directory"""
    return glob.glob(join(TESTDIR, '*.tests'))
    
    
if __name__=='__main__':
    def suite():
        suite = unittest.TestSuite()
        for name in getDocTests():
            suite.addTest(doctest.DocFileSuite(name, module_relative=False, setUp=setUp))
        for mod in getModules():
            try:
                suite.addTest(doctest.DocTestSuite(mod, extraglobs=initGlobs(), setUp=setUp))
            except ValueError, e:
                print >>sys.stderr, e
        return suite
    unittest.main(defaultTest='suite')
#     runner = unittest.TextTestRunner()
#     runner.run(suite)
